using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms ;
using System.Collections;
using System.Xml;
using System.Reflection;

namespace Client
{
    public class TypeCodeComboBox<TElement> : ComboBox 
        where TElement : new ()
    {
        private string _AcordMetafileName = "..\\..\\ACORD_Life_Standards_Metadata2.13.00.xml";

        public TypeCodeComboBox() 
        {
            this.DisplayMember = "Key";
            this.ValueMember = "Value";

            // Open file containing ACORD OLifE code metadata

            System.Xml.XmlDocument doc = new XmlDocument();
            doc.Load(_AcordMetafileName);

            // Assemble Xpath query string to return the set of nodes that we 
            // want from the document. It is fortunate that in XMLife, the name of the
            // type code lookup is the same as the element, with the characters _TC appended.
            // In the metadata file, the name of the element is the name of the type code section

            string xq = "/ACORD-XML-DOC/DataTypes/DataType[TypeName='" + typeof(TElement).Name + "']/Enumeration/Codes/Code";
            
            // Perform the query that gives us the resulting node set

            XmlNodeList nl = doc.SelectNodes(xq);

            // For each node, find the name and the value. Place into dictionary entries
            // and add to ComboBox

            foreach (XmlNode n in nl)
            {
                string name = n.SelectSingleNode("Desc/Description").InnerText;
                int code = Convert.ToInt32(n.SelectSingleNode("Value").InnerText);
                this.Items.Add(new DictionaryEntry(name, code ));
            }

            // The XML document doesn't have a close method, we just have to let it go out 
            // of scope and be garbage collected. 

            this.BackColor = System.Drawing.Color.Azure;

        }

        public TElement SelectedElement
        {
            get
            {
                TElement el = new TElement();
                int tc = (int) ((DictionaryEntry) this.SelectedItem).Value ;

                Type t = el.GetType();
                PropertyInfo pi = t.GetProperty("tc");
                pi.SetValue(el, tc, null);

                return el;
            }

            //get
            //{
            //    TElement el = new TElement();
            //    string tc = ((DictionaryEntry)this.SelectedItem).Value.ToString();

            //    Type t = el.GetType();
            //    PropertyInfo pi = t.GetProperty("tc");
            //    pi.SetValue(el, tc, null);

            //    return el;
            //}
        }

        public TElement SelectedElementWithText
        {
            get
            {
                TElement el = this.SelectedElement;
                string text = (string)((DictionaryEntry)this.SelectedItem).Key;

                Type t = el.GetType();
                PropertyInfo pi = t.GetProperty("Value");
                pi.SetValue(el, text, null);

                return el;
            }
        }

        public string AcordMetadataFileName
        {
            set { _AcordMetafileName = value; }
            get { return _AcordMetafileName; }
        }

        public void ReplacePlainComboBox(ref ComboBox oldone)
        {

            // Add new one to the parent that holds the old one

            oldone.Parent.Controls.Add(this);

            // copy properties from old to new

            this.FormattingEnabled = oldone.FormattingEnabled;
            this.Location = oldone.Location;
            this.Name = oldone.Name + "New";
            this.Size = oldone.Size;
            this.TabIndex = oldone.TabIndex;

            // Take old control out of array

            oldone.Parent.Controls.Remove(oldone);

            // dispose of old one

            oldone.Dispose();

            // null reference in the form so that it isn't used inadvertantly 

            oldone = null;

        }
 
     }
}
